package com.etcfg.etlaunch;

import java.awt.Component;
import java.awt.Desktop;
import java.awt.Window;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

public class UpdateChecker implements Runnable {

	private Window parent;
	private boolean silent = false;

	public Component getParent() {
		return parent;
	}

	public void setParent(Window parent) {
		this.parent = parent;
	}

	public boolean isSilent() {
		return silent;
	}

	public void setSilent(boolean silent) {
		this.silent = silent;
	}

	public void run() {
		String updateUrl = "http://etcfglaunch.googlecode.com/svn/com.etcfg.launch/trunk/com.etcfg.launch/latestversion.txt";
		try {

			ByteArrayOutputStream baos = new ByteArrayOutputStream();

			FileDownloader fileDownloader;
			if (silent) {
				fileDownloader = new FileDownloader(updateUrl, baos);
				fileDownloader.downloadFile();

			} else {
				fileDownloader = new FileDownloader(updateUrl, baos,
						"Checking for updates ", parent, true);
				fileDownloader.startDownload();
			}

			InputStreamReader isr = new InputStreamReader(
					new ByteArrayInputStream(baos.toByteArray()), "UTF-8");
			BufferedReader bufferedReader = new BufferedReader(isr);
			final String version = bufferedReader.readLine().trim();
			final String file = bufferedReader.readLine().trim();
			StringBuilder changes = new StringBuilder();
			String line;
			while ((line = bufferedReader.readLine()) != null) {
				changes.append(line);
				changes.append("\n");
			}

			bufferedReader.close();
			isr.close();
			if (ApplicationSetting.VERSION.equals(version)) {
				System.out.println("No new version found");
				SwingUtilities.invokeLater(new Runnable() {

					@Override
					public void run() {
						if (!silent) {
							JOptionPane.showMessageDialog(parent,
									"You running on the latest version!");
						}
					}

				});
			} else {

				SwingUtilities.invokeAndWait(new Runnable() {

					@Override
					public void run() {
						int showConfirmDialog = JOptionPane
								.showConfirmDialog(
										parent,
										"New version "
												+ version
												+ " is found, do you want to download it?",
										"Message", JOptionPane.YES_NO_OPTION);
						if (showConfirmDialog == JOptionPane.YES_OPTION) {

							JFileChooser chooser = new JFileChooser();
							chooser.setSelectedFile(new File(System
									.getProperty("user.home"),
									"etcfgLaunch_setup_v" + version + ".exe"));
							int showSaveDialog = chooser.showSaveDialog(parent);
							if (showSaveDialog == JFileChooser.APPROVE_OPTION) {
								File selectedFile = chooser.getSelectedFile();
								FileOutputStream fileOutputStream;
								try {
									fileOutputStream = new FileOutputStream(
											selectedFile);
									FileDownloader downloader = new FileDownloader(
											file, fileOutputStream,
											"Downloading etcfgLaunch "
													+ version, parent, true);
									downloader.startDownload();

									fileOutputStream.close();
									if (downloader.isCanceled() == false
											&& downloader.getException() == null) {
										Desktop.getDesktop().browse(
												selectedFile.toURI());

										System.exit(1);
									}

								} catch (FileNotFoundException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								} catch (IOException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}

							}
						}

					}

				});
			}
		} catch (Exception e1) {
			e1.printStackTrace();
			SwingUtilities.invokeLater(new Runnable() {

				@Override
				public void run() {
					if (!silent) {
						JOptionPane.showMessageDialog(parent,
								"Error, Couldn't check for updates=(");
					}
				}
			});
		}
	}
}
